package gvo.util;

import gvo.basicDatatypes.GVOall;
import gvo.biblio.GVO_GUI;
import gvo.dbinfo.DBInfo;
import gvo.image.try2.ImageViewer;
import gvo.importWindow.InternetImport;
import gvo.presets.Presets;
import gvo.sql.SQLWizard;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

public class GVOstore {
	public static final String VERSION = "1.3.3";
	

	public static final int NOT_DEF = 0;
	public static final int OK_DB = 1;
	public static final int DRIVER_ERROR = -1;
	public static final int CONN_ERROR = -2;
	
	public static final int AMAZON = 0;
	public static final int RERO = 1;
	public static final int DBWINDOW = 2;
	private int dbStatus;
	private boolean dbready = false;

	//properties start
	public Presets pre;
	//properties end
	
	private boolean dropTables = false;
	private boolean dropViews = false;
	private boolean initTables = true;
	private boolean initViews = true;
	
	
	private boolean recycleWindowRI = true;
	
	private GVO_GUI gdw;
	private GVO_GUI gvoGUI;
	private static final boolean TESTERRORLEVELS = false; 
	//private ReroImport reroimp;
	private InternetImport internetimp;
	private InternetImport internetimpRERO;
	private ImageViewer iv;
	private DBInfo dbInfo = new DBInfo();
	private Vector<String> strQuerys = new Vector<String>();
	public SQLWizard sqlTool = new SQLWizard(this);
	public ArrayList<Integer> buIds = new ArrayList<Integer>();
	
	public static final Color FeldhintergrundRERO = new Color(170, 255, 153);
	public static final Color FeldhintergrundDB = new Color(148, 255, 255);
	//DB Start Info
	public static final boolean DBSTARTINFO = false;
	
	public GVOstore() {
		pre = new Presets();
		
		if(TESTERRORLEVELS){
			GVOstore.showMessage("Finest", Level.FINEST ,GVOstore.class.getName());
			GVOstore.showMessage("Finer", Level.FINER ,GVOstore.class.getName());
			GVOstore.showMessage("Fine", Level.FINE ,GVOstore.class.getName());
			GVOstore.showMessage("Config", Level.CONFIG ,GVOstore.class.getName());
			GVOstore.showMessage("Info", Level.INFO ,GVOstore.class.getName());
			GVOstore.showMessage("Warning", Level.WARNING ,GVOstore.class.getName());
			GVOstore.showMessage("Severe", Level.SEVERE ,GVOstore.class.getName());
		}
		if (sqlTool.startdb()) {
			sqlTool.getidBuecher();
			GVOstore.showMessage("anz DS="+buIds.size(), Level.CONFIG, GVOstore.class.getName());
			sqlTool.getTables();
			getDbInfo().fillSelectTable(this);
		} else {
			GVOstore.showMessage("Fehler: DB nicht gestartet!", Level.WARNING, GVOstore.class.getName());
		}
		
	}
	
	public Point getLocation(JFrame parent) {
		Point p;
		if (parent == null){
			p = null;
		} else {
			p = parent.getLocation();
			p.x = p.x + parent.getWidth()+1;
			
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			if (p.x +this.pre.getWGVOguiD() > screenSize.width){
				p.x=0;
			}
		}
		return p;
	}
	public void showReroResults(int index, GVOall gvoall){
		//System.out.println("Loc=" +loc );
		GVO_GUI zeigeTmp;
		if(recycleWindowRI){
			if(getGdw() == null){
				//System.out.println("RECYCLE");
				//int index, GVOall gvoall, GVOstore gvostore, ReroImport ri, Point loc
				setGdw(new GVO_GUI(index, gvoall, this));
			}
			zeigeTmp = getGdw();
		}else{ 
			setGdw(null);
			zeigeTmp = new GVO_GUI(index, gvoall, this);
		}
		zeigeTmp.changeDS(index, gvoall);
		zeigeTmp.toFront();
		
	}
	public void showInternetResults(int index, GVOall gvoall, int art){
		
		//System.out.println("Loc=" +loc );
		GVO_GUI zeigeTmp;
		if(recycleWindowRI){
			if(getGdw() == null){
				//System.out.println("RECYCLE");
				//int index, GVOall gvoall, GVOstore gvostore, ReroImport ri, Point loc
				setGdw(new GVO_GUI(index, gvoall, this, art));
			}
			zeigeTmp = getGdw();
		}else{ 
			setGdw(null);
			zeigeTmp = new GVO_GUI(index, gvoall, this, art);
		}
		zeigeTmp.setVisible(true);
		zeigeTmp.changeDS(index, gvoall);
		zeigeTmp.toFront();
		
		
	}
	
	/**
	 * @param msg
	 *  the message
	 * @param level (use from Config - Severe!)
	 *   SEVERE (highest value), 
	 *   WARNING, 
	 *   INFO, 
	 *   CONFIG, 
	 *   FINE, 
	 *   FINER, 
	 *   FINEST (lowest value), 
	 *   In addition there is a level OFF that can be used to turn off logging, and a level ALL that can be used to enable logging of all messages.
	 * @param classname
	 * the name of the class, where the msg comes from
	 */
	public static void showMessage(String msg, Level level,  String classname){
		Logger log = Logger.getLogger(classname);
		log.log(level, msg);
	}
	
	public void setDropTables(boolean dropTables) {
		this.dropTables = dropTables;
	}
	public boolean isDropTables() {
		return dropTables;
	}
	public void setInitTables(boolean initTables) {
		this.initTables = initTables;
	}
	public boolean isInitTables() {
		return initTables;
	}
	public void setInitViews(boolean initViews) {
		this.initViews = initViews;
	}
	public boolean isInitViews() {
		return initViews;
	}
	public void setDropViews(boolean dropViews) {
		this.dropViews = dropViews;
	}
	public boolean isDropViews() {
		return dropViews;
	}
	public void setDbInfo(DBInfo dbInfo) {
		this.dbInfo = dbInfo;
	}
	public DBInfo getDbInfo() {
		return dbInfo;
	}
	public void setGdw(GVO_GUI gdw) {
		this.gdw = gdw;
	}
	public GVO_GUI getGdw() {
		return gdw;
	}
	public void setGvoGUI(GVO_GUI gvoGUI) {
		this.gvoGUI = gvoGUI;
	}
	public GVO_GUI getGvoGUI() {
		return gvoGUI;
	}
	public void setInternetimp(InternetImport internetimp) {
		this.internetimp = internetimp;
	}
	public InternetImport getInternetimp() {
		return internetimp;
	}
	public void setInternetimpRERO(InternetImport internetimpRERO) {
		this.internetimpRERO = internetimpRERO;
	}
	public InternetImport getInternetimpRERO() {
		return internetimpRERO;
	}
	public void setIv(ImageViewer iv) {
		this.iv = iv;
	}
	public ImageViewer getIv() {
		return iv;
	}
	public void setStrQuerys(Vector<String> strQuerys) {
		this.strQuerys = strQuerys;
	}
	public Vector<String> getStrQuerys() {
		return strQuerys;
	}
	public void setDbready(boolean dbready) {
		this.dbready = dbready;
	}
	public boolean isDbready() {
		return dbready;
	}
	public void setDbStatus(int dbStatus) {
		this.dbStatus = dbStatus;
	}
	public int getDbStatus() {
		return dbStatus;
	}
}
